# Figure B3: Generalized fluctuation test for structural breaks

rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(ggplot2)
library(strucchange)
library(stringr)

crimes <- fread('./data/2007_2020_hate_crimes_classified.csv')

crimes[region_broad!='',label:='region of origin']
crimes[immigrant!='' & region_broad=='', label:='immigrant']
crimes[(religion_broad!='' | fascism!='') & (region_broad=='' & region_broad==''), label:='religion']
crimes[is.na(label), label:='other']

crimes[,date:=as.Date(crimes$Date, '%d-%m-%Y')]
crimes[,month:=as.Date(cut(date,breaks = "month", start.on.monday = FALSE))]

# Figure B3 (a)
## break in trend
asian <- crimes[region_broad=='Asia']
asian <- asian[,.N,by=month]
months <- crimes[,.N,by=month]
setnames(months,'N','all')
asian <- merge(months,asian,by='month', all.x = T)
asian[is.na(N), N:=0]
asian_ts <- ts(asian[,N], start=c(2007, 1), end=c(2020, 12), frequency=12) 

re.seat <- efp(asian_ts ~ 1, type='OLS-MOSUM', h=0.02)

## dating the structural change
bp.seat <- breakpoints(asian_ts ~ 1, type='OLS-MOSUM', h = 0.02)

## plot structural change 
pdf('./asian_trend_break_all_2020_months.pdf', width=6, height=4.5)
plot(re.seat, xlab=NULL, main = NA, axes=F, ylab='Asian crimes empirical fluctuation process')
box(bty="l")
axis(2, c(-0.8, 0, 0.8))
axis(1)
lines(bp.seat, breaks = 1,col='blue')
dev.off()

# Chow test on Jan, 2020
sctest(asian_ts ~ 1,
       type = "Chow", point = 157)


# Figure 3B (b)
african <- crimes[region_broad=='Africa']
african <- african[,.N,by=month]
months <- crimes[,.N,by=month]
setnames(months,'N','all')
african <- merge(months,african,by='month', all.x = T)
african[is.na(N), N:=0]
african_ts <- ts(african[,N], start=c(2007, 1), end=c(2020, 12), frequency=12) 

## testing for structural change (residuals)
re.seat <- efp(african_ts ~ 1, type='OLS-MOSUM', h=0.02)

## dating the structural change
bp.seat <- breakpoints(african_ts ~ 1, type='OLS-MOSUM', h = 0.02)

## plot structural change 
pdf('./african_trend_break_all_2020_months.pdf', width=6, height=4.5)
plot(re.seat, xlab=NULL, main = NA, axes=F, ylab='African crimes empirical fluctuation process')
box(bty="l")
axis(2, c(-0.8, 0, 0.8))
axis(1)
lines(bp.seat, breaks = 1,col='blue')
dev.off()

# Chow test on Jan, 2020
sctest(african_ts ~ 1,
       type = "Chow", point = 157)

### Chow-tests on structural breaks on January 2020 for all groups by group
regions <- crimes[,unique(region_broad)]

chow <- vector(mode = 'list', length = length(regions))
region <- vector(mode = 'list', length = length(regions))

for (i in regions) {
  j <- match(i,regions)
  crimes_region <- crimes[region_broad==i]
  crimes_region <- crimes_region[,.N,by=month]
  months <- crimes[,.N,by=month]
  setnames(months,'N','all')
  crimes_region <- merge(months,crimes_region,by='month', all.x = T)
  crimes_region[is.na(N), N:=0]
  ts <- ts(crimes_region[,N], start=c(2007, 1), end=c(2020, 12), frequency=12) 
  
  chow[[j]] <- sctest(ts ~ 1,
                      type = "Chow", point = 157)$p.value
  region[[j]] <- i
}

chow_test <- data.frame(p_value=unlist(chow), origin=unlist(region), n=crimes[,.N,by=region_broad]$N)

# Change in crime before after January 2020 for group with break in trend according to Chow test
# hate crime rate is decreasing in Eastern and Southern Europe, as opposed to increasing
crimes[region_broad=='Eastern and Southern Europe' & month<'2020-01-01' & month>='2019-01-01', .N]/length(crimes[month<'2020-01-01' & month>='2019-01-01',unique(month)])
crimes[region_broad=='Eastern and Southern Europe' & month>'2020-01-01', .N]/length(crimes[month>'2020-01-01',unique(month)])
